{include file="public/header" /}
<script type="text/javascript" src="{$Think.HTTP_TYPE}api.map.baidu.com/api?v=2.0&ak=22bb7221fc279a484895afcc6a0bb33a">
</script>
<link rel="stylesheet" href="{$Think.HOME_SITE_ROOT}/css/common.css">
<script>
    var local;
    var map;
    var lst_name = '';

    function change_map() {
        if ($("#area_ select:eq(0)").length > 0 && $("#area_ select:eq(0) option:selected").val() != '') {
            var name = $("#area_ select:eq(0) option:selected").text();
        }
        if ($("#area_ select:eq(1)").length > 0 && $("#area_ select:eq(1) option:selected").val() != '') {
            var name = $("#area_ select:eq(1) option:selected").text();
        }
        if ($("#area_ select:eq(2)").length > 0 && $("#area_ select:eq(2) option:selected").val() != '') {
            var name = $("#area_ select:eq(2) option:selected").text();
        }
        if (name != '' && lst_name != name) {
            lst_name = name;
            map.setCurrentCity(name);
            map.centerAndZoom(name, 16);
            map.clearOverlays();
            local.search(name);
        }

    }
    $(function () {
        map = new BMap.Map("allmap");
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var lng = r.point.lng;
                var lat = r.point.lat;
                var point = new BMap.Point(lng, lat);
                map.centerAndZoom(point, 16);
                map.addControl(new BMap.NavigationControl());
                map.enableScrollWheelZoom();
                var marker = new BMap.Marker(point); // 创建标注
                map.addOverlay(marker); // 将标注添加到地图中
                marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                document.getElementById("longitude").value = lng;
                document.getElementById("latitude").value = lat;

            } else {
                layer.msg('failed' + this.getStatus());
            }
        }, {
            enableHighAccuracy: true
        })

        var options = {
            onSearchComplete: function (results) {
                // 判断状态是否正确
                if (local.getStatus() == BMAP_STATUS_SUCCESS) {
                    if (results.getCurrentNumPois() > 0) {

                        map.clearOverlays(); //清除标注  或者可以把market 放入数组
                        var point = new BMap.Point(results.getPoi(0).point.lng, results.getPoi(0).point
                            .lat);
                        var marker = new BMap.Marker(point);
                        map.centerAndZoom(point, 16);
                        map.addOverlay(marker);
                        marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画

                        document.getElementById("longitude").value = results.getPoi(0).point.lng;
                        document.getElementById("latitude").value = results.getPoi(0).point.lat;

                    }
                }
            }
        };
        local = new BMap.LocalSearch(map, options);
        map.addEventListener("click", function (e) {
            //                    alert(e.point.lng + ", " + e.point.lat);
            map.clearOverlays(); //清除标注  或者可以把market 放入数组
            var point = new BMap.Point(e.point.lng, e.point.lat);
            var marker = new BMap.Marker(point);
            map.addOverlay(marker);
            marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画

            document.getElementById("longitude").value = e.point.lng;
            document.getElementById("latitude").value = e.point.lat;
        });
    })
</script>

<style>
    ul>li {
        width: auto !important;
    }
</style>


<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <div class="subject">
                <h3>添加二手商品</h3>
            </div>
            {include file="public/admin_items" /}
        </div>
    </div>

    <form id="add_form" enctype="multipart/form-data" method="post" action="{url('Twogoods/two_goods_add')}"
        onSubmit="return confirm();">
        <table class="ds-default-table">
            <tbody>
                <tr>
                    <td colspan="2" class="required"><label class="validation">商品名称:</label>
                        <input type="text" value="" name="goods_name" id="gc_name"
                            style="width: 200px; margin-left: 60px;" class="txt"><span></span></td>
                    <td class="vatop tips"></td>
                </tr>
                <tr>
                    <td colspan="2" class="required"><label>商品分类:</label>
                        <div id="gcategory" style="margin-left: 134px;margin-top:-20px; ">
                            <input type="hidden" name="class_id" value="{$type.class_id}" class="mls_id"
                                id="class_id" />
                            <input type="hidden" name="class_name" value="{$type.class_name|default=''}"
                                class="mls_name" />
                            {if $type.class_id}
                            <span>{$type.class_name|default=''}</span>
                            <input type="button" value="{$Think.lang.ds_edit}" class="edit_gcategory" />
                            {/if}
                            <select {if $type.class_id}style="display:none" {/if}> <option value="0">请选择</option>
                                {foreach name="gc_list" item="gc"}
                                <option value="{$gc.gc_id}">{$gc.gc_name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </td>
                </tr>
                <tr>
                    <!--color: #0A0A0A-->
                    <td colspan="2"><label class="validation">商品图片:</label>
                        <input name="pic[]" type="file" id="upload_img" url="{$Think.UPLOAD_SITE_URL}" size="30"
                            style="margin-left: 35px;" />
                    </td>
                    <td>
                        <span data_id='-1'></span>

                    </td>
                </tr>
                <tr>
                    <!--color: #0A0A0A-->
                    <td>
                        提示：包含设备外观照片，铭牌照片，重要部件照片，现在状态照片；
                    </td>
                    <td colspan="2"> </td>
                </tr>
                <tr>
                    <td colspan="2"><label>商品品牌:</label>
                        <div style='margin-left: 128px;margin-top: -18px;' id="ajax_brand"
                            class="dssc-brand-select w180">
                            <div class="selection">
                                <input name="b_name" id="b_name" value="{$Request.param.b_name|default=''}" type="text"
                                    class="txt w180" readonly="readonly" />
                                <input type="hidden" name="b_id" id="b_id" value="{$Request.param.b_id|default=''}" />
                            </div>
                            <div class="dssc-brand-select-container">
                                <div class="brand-index"
                                    style="width: 210px;padding-bottom: 10px;margin: 6px auto;border-bottom: dotted 1px #CCC;"
                                    data-url="{:url('Common/ajax_get_brand')}">
                                    <div class="letter" dstype="letter">
                                        <ul style="width: 100%;">
                                            <li style="width: auto;"><a href="javascript:void(0);"
                                                    data-letter="all">所有品牌</a></li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="A">A</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="B">B</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="C">C</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="D">D</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="E">E</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="F">F</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="G">G</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="H">H</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="I">I</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="J">J</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="K">K</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="L">L</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="M">M</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="N">N</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="O">O</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="P">P</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="Q">Q</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="R">R</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="S">S</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="T">T</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="U">U</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="V">V</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="W">W</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="X">X</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="Y">Y</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);" data-letter="Z">Z</a>
                                            </li>
                                            <li style="width: auto;"><a href="javascript:void(0);"
                                                    data-letter="0-9">其他</a></li>
                                        </ul>
                                    </div>
                                    <div class="search" dstype="search"><input name="search_brand_keyword"
                                            id="search_brand_keyword" type="text" class="text" /><a
                                            href="javascript:void(0);" class="dssc-btn-mini"
                                            style="vertical-align: top;">Go</a></div>
                                </div>
                                <div class="brand-list" dstype="brandList">
                                    <ul dstype="brand_list">
                                        {notempty name="brand_list"}
                                        {foreach name="brand_list" item="val"}
                                        <li data-id='{$val.brand_id}' data-name='{$val.brand_name}'>
                                            <em>{$val.brand_initial}</em>{$val.brand_name}</li>
                                        {/foreach}
                                        {/notempty}
                                    </ul>
                                </div>
                                <div class="no-result" dstype="noBrandList" style="display: none;">
                                    {$Think.lang.no_brand_result}</div>
                            </div>
                        </div>


                    </td>
                    <!--  <td class="vatop tips">{$Think.lang.goods_class_add_sup_class_notice}</td> -->
                </tr>

                <tr>
                    <td colspan="2" class="required"><label class="validation">数量:</label>

                        <input class="w60" type="text" name="count" style="margin-left: 80px;"><span></span></td>
                    <!-- <td class="vatop tips">{$Think.lang.commis_rate_tips}</td> -->
                </tr>
                <tr>
                    <td colspan="2" class="required"><label class="validation">售价:</label>
                        <input class="w60" type="text" name="price" style="margin-left: 80px;"><span></span></td>
                    <td class="vatop tips"></td>
                </tr>
                <tr>
                    <td><label class="validation">所在地:</label>
                        <p colspan="2" class="required" id="area_" style="margin-left: 124px;margin-top: -19px;">
                            <input type="hidden" value="" name="company_address" id="company_address">
                            <input type="hidden" value="" name="province_id" id="_area_1">
                            <input type="hidden" value="" name="city_id" id="_area_2">
                            <input type="hidden" value="" name="district_id" id="_area_3"></p>
                    </td>

                </tr>
                <tr>
                    <td colspan="2" class="required"><label class="validation">联系方式:</label>
                        <input id="commis_rate" class="w360" type="text" name="phone"
                            style="margin-left: 47px;"><span></span></td>
                    <td class="vatop tips"></td>
                </tr>
                <tr>
                    <td colspan="2" class="required"><label class="validation">说明:</label>
                        <textarea name="content" id="" cols="30" rows="10"
                            style="margin-left: 69px;"></textarea><span></span></td>
                    <td class="vatop tips"></td>
                </tr>
                <tr>
                    <!--color: #0A0A0A-->
                    <td>
                        提示：产品购买和使用时间，产品维修保养记录，产品现状介绍；
                    </td>
                    <td colspan="2"> </td>
                </tr>
                <tr>
                    <td colspan="2" class="validation"><label>商品状态:</label>
                        <select name="goods_status" id="" style="margin-left: 58px;">
                            <option value="1">上架</option>
                            <option value="3">下架</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" class="validation"><label>是否可议价:</label>
                        <select name="two_is_price" style="margin-left: 58px;">
                            <option value="0">不可</option>
                            <option value="1">可以</option>
                        </select>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="2"><input class="btn" type="submit" value="{$Think.lang.ds_submit}"
                            style='width:100px; margin-left:70px;' /></td>
                </tr>
            </tfoot>
        </table>
    </form>
</div>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/mlselection.js" charset="utf-8"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/jquery.mousewheel.js"></script>
<script>
    var http_name = "{$http_name}";
    //按钮先执行验证再提交表单
    $(document).ready(function () {

        //图片删除
        $(document).on('click', '.del_img', function () {
            /* var lastId = $(this).parent().parent().parent().find('tr').last().attr('data-id');
             html+="<input type='hidden' name='area_id["+id+"]' value='"+chk_value+"'>";*/
            var data_id = $(this).attr('data_id');
            var data_url = $(this).attr('data_url');
            var html = "<input type='hidden' name='del_pic[" + data_id + "]' value='" + data_url +
                "' />";
            $(this).parent().remove();
        });


        //图片上传并展示
        $(document).on('change', '#upload_img', function () {
            var files = document.getElementById('upload_img').files[0];
            var lastId = $(this).parent().next().find('span').last().attr('data_id');
            var lastid = lastId + 1;
            var url = $(this).attr('url');
            var obj = $(this).parent().next();
            var formData = new FormData();
            formData.append('file', files);
            $.ajax({
                //请求方式
                type: "POST",
                //请求地址
                url: "ajax_upload",
                data: formData,
                cache: false, //默认是true，但是一般不做缓存
                processData: false, //用于对data参数进行序列化处理，这里必须false；如果是true，就会将FormData转换为String类型
                contentType: false, //一些文件上传http协议的关系，自行百度，如果上传的有文件，那么只能设置为false
                //请求成功
                success: function (result) {
                    if (result.err == 0) {
                        $('#upload_img').val('');
                        layer.msg(result.msg);
                        var html = "<span data_id='" + lastid + "'><img src='" + http_name +
                            '/' + result.data +
                            "' width='120' height='120'/><input type='hidden' name='pic[" +
                            lastid + "]'  value='" + result.data +
                            "'  /><input type='button' value='删除' class='del_img' data_id='" +
                            lastid + "' data_url='" + result.data + "'/></span>";
                        obj.append(html);
                    } else {
                        $('#upload_img').val('');
                        alert('文件信息错误,请重新上传');
                        return false;
                    }



                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                    console.log(e.status);
                    console.log(e.responseText);
                }
            });

        });





        /* AJAX选择品牌 */
        $("#ajax_brand").brandinit();

        $('#dssubmit').click(function () {
            $('#formSearch').submit();
        });

        $('#dssubmit').click(function () {
            $('#formSearch').submit();
        });
        $('#company_address').ds_region();
    });
    $(function () {
        // 类型搜索
        $(document).off('change', "#gcategory > select").on("change", "#gcategory > select", function () {
            type_scroll($(this));
        });
        var typeScroll = 0;

        function type_scroll(o) {
            var id = o.val();
            if (!$('#type_dt_' + id).is('dt')) {
                return false;
            }
            $('#type_div').scrollTop(-typeScroll);
            var sp_top = $('#type_dt_' + id).offset().top;
            var div_top = $('#type_div').offset().top;
            $('#type_div').scrollTop(sp_top - div_top);
            typeScroll = sp_top - div_top;
        }
        gcategoryInit('gcategory');


        $('#area_id1').change(function () {
            var htmls = '<option value="0">请选择...</option>';
            $('#area_id2').html(htmls);
            $('#area_id3').html(htmls);
            var area_id = $(this).val();
            if (area_id == '0') {
                var htmls = '<option value="0">请选择...</option>';
                $('#area_id2').html(htmls);
                $('#area_id3').html(htmls);
            } else {
                $.ajax({
                    //请求方式
                    type: "POST",
                    //请求地址
                    url: "ajax_get_area_class",
                    //数据，json字符串
                    data: {
                        'area_id': area_id
                    },
                    //请求成功
                    success: function (result) {
                        var htmls = '<option value="0">请选择...</option>';
                        $.each(result, function (item, v) {
                            htmls += "<option value='" + v.area_id + "'>" + v
                                .area_name + "</option>";
                        });

                        $('#area_id2').html(htmls);
                    },
                    //请求失败，包含具体的错误信息
                    error: function (e) {
                        console.log(e.status);
                        console.log(e.responseText);
                    }
                });
            }
        });

        $('#area_id2').change(function () {
            var htmls = '<option value="0">请选择...</option>';
            var area_id = $(this).val();

            $('#area_id3').html(htmls);
            if (area_id == '0') {

                /*  $('#gc_id2').html(htmls);*/
                $('#area_id3').html(htmls);
            } else {
                $.ajax({
                    //请求方式
                    type: "POST",
                    //请求地址
                    url: "ajax_get_area_class",
                    //数据，json字符串
                    data: {
                        'area_id': area_id
                    },
                    //请求成功
                    success: function (result) {
                        var htmls = '<option value="0">请选择...</option>';
                        $.each(result, function (item, v) {
                            htmls += "<option value='" + v.area_id + "'>" + v
                                .area_name + "</option>";
                        });

                        $('#area_id3').html(htmls);
                    },
                    //请求失败，包含具体的错误信息
                    error: function (e) {
                        console.log(e.status);
                        console.log(e.responseText);
                    }
                });
            }
        });

        jQuery.validator.addMethod("checkFCodePrefix", function (value, element) {
            return this.optional(element) || /^[a-zA-Z]+$/.test(value);
        }, '<i class="iconfont">&#xe64c;</i>{$Think.lang.prompt_information1}');
        $("#add_form").validate({
            errorPlacement: function (error, element) {
                $(element).next('span').append(error);
            },
            rules: {
                content: {
                    required: true
                },
                goods_name: {
                    required: true
                },
                count: {
                    required: true,
                    digits: true,
                    min: 1
                },
                price: {
                    required: true,
                    number: true,
                    min: 0.01
                },
                phone: {
                    required: true,
                    minlength: 11,
                    maxlength: 11,
                }
            },
            messages: {
                content: {
                    required: "请填写描述"
                },
                goods_name: {
                    required: "不能为空"
                },
                count: {
                    required: "不能为空",
                    digits: "请设置大于0的整数",
                    min: '请设置大于0的整数'
                },
                price: {
                    required: "不能为空",
                    number: "请设置大于0的整数",
                    min: '请设置大于0的整数'
                },
                phone: {
                    required: "请输入手机号",
                    minlength: "不能小于11个字符",
                    maxlength: "不能大于11个字符",
                }
            }
        });

    });

    function confirm() {
        var _area_3 = $('#_area_3').val();
        var class_id = $('#class_id').val();
        var lastId = $('#upload_img').parent().next().find('span').last().attr('data_id');

        if (_area_3 == '') {
            layer.msg('请选择所在地');
            return false;
        }
        if (class_id == '0') {
            layer.msg('请选择分类');
            return false;
        }
        if (lastId == '-1') {
            layer.msg('请上传图片');
            return false;
        }
        var res = $('form').valid();
        if (res) {
            var url = $('form').attr('action');
            //alert(url);return false;
            // alert(url);return false;
            $.ajax({
                type: "POST",
                url: url,
                data: $('#add_form').serialize(), // 你的formid

                error: function (request) {
                    alert("Connection error");
                },
                success: function (data) {
                    if (data.code == 0) {

                        layer.msg(data.msg);
                        location.href = data.url;
                        return false;
                    } else {
                        layer.msg(data.msg);
                        location.href = data.url;
                        return false;

                    }
                }
            });
            return false;
        } else {
            layer.msg('操作失败');
            return false;
        }
    }
</script>

{include file="public/footer" /}